package com.ruoyi.fjtseac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.fjtseac.domain.dto.AudioDto;
import com.ruoyi.fjtseac.domain.po.Audio;
import com.ruoyi.fjtseac.domain.vo.AudioVo;
import com.ruoyi.fjtseac.mapper.AudioMapper;
import com.ruoyi.fjtseac.service.AudioService;
import com.ruoyi.fjtseac.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.ruoyi.common.utils.PageUtils.startPage;
import static com.ruoyi.fjtseac.util.Constants.FALSE;

/**
 * @author zzl
 * @date 2022/7/7 16:57
 */
@Service
public class AudioServiceImpl extends ServiceImpl<AudioMapper, Audio> implements AudioService {

    @Value("${cos.host}")
    private String host;
    @Autowired
    private FileUtil fileUtil;
    @Autowired
    private LocalFile localFile;
    @Autowired
    private AudioMapper audioMapper;

    @Override
    public AjaxResult upload(@NotNull(message = "音频对象不能为空") AudioDto audioDto, String uuid) {
        MultipartFile file = audioDto.getAudioFile();
        String audioUrl = fileUtil.upload(file, Constants.AUDIO, uuid);
        if (Objects.equals(audioUrl, FALSE)) {
            return AjaxResult.error("音频文件因某种原因上传失败，请重新上传");
        }
        String time = localFile.getFileTime(audioDto.getAudioFile(), Constants.AUDIO);
        if (time == null) {
            return AjaxResult.error("获取音频时长失败");
        }
        Audio audio = DtoToPoMapper.INSTANCE.toAudio(audioDto);
        audio.setAudioUrl(audioUrl);
        audio.setDuration(time);
        int n = audioMapper.insert(audio);
        if (n == 0) {
            fileUtil.delete(audioUrl.replaceAll(host + "/", ""));
            return AjaxResult.error("数据插入失败");
        }

        return AjaxResult.success();
    }

    @Override
    public AjaxResult delete(Integer id) {
        Audio audio = audioMapper.selectById(id);
        if (audio == null) {
            return AjaxResult.error("音频记录不存在");
        }
        audioMapper.deleteById(id);
        if (!fileUtil.delete(audio.getAudioUrl().replaceAll(host + "/", ""))) {
            return AjaxResult.success("音频记录删除成功，但音频文件删除失败");
        }
        return AjaxResult.success();
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public AjaxResult search(String param, int pageNum, int pageSize) {
        startPage(pageNum, pageSize);
        QueryWrapper<Audio> wrapper = new QueryWrapper<>();
        wrapper.like(param != null, "audio_name", param)
                .orderBy(true, false, "create_time");

        List<Audio> audios = audioMapper.selectList(wrapper);
        List<AudioVo> audioVos = new ArrayList<>();
        for (Audio audio : audios) {
            AudioVo audioVo = PoToVoMapper.INSTANCE.toAudioVo(audio);
            audioVos.add(audioVo);
        }
        return AjaxResult.getPageResult(audios, audioVos);
    }

    @Override
    public AjaxResult getProgress(String uuid) {
        return AjaxResult.success(fileUtil.getProgress(uuid));
    }

}
